package com.apex.util;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class PddCrawler {

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        String url = "https://mobile.yangkeduo.com/search_result.html?search_key=手机";
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        String html = response.body().string();

        Document doc = Jsoup.parse(html);
        Elements products = doc.select("ul li a.goods-link"); // 获取商品列表

        // 遍历商品列表，获取商品信息
        for (Element product : products) {
            String name = product.selectFirst("div.goods-name").text();
            String price = product.selectFirst("span.price").text();
            String sales = product.selectFirst("span.sales").text();

            System.out.println("商品名称：" + name + "，商品价格：" + price + "，销售数量：" + sales);
        }
    }
}